package com.friend.ui;

import android.Manifest;
import android.util.Log;
import android.view.View;
import android.widget.RadioButton;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import com.alibaba.android.arouter.launcher.ARouter;
import com.friend.R;
import com.friend.entity.User;
import com.friend.gen.UserDao;
import com.friend.ui.activity.presenter.LoginPresenter;
import com.friend.ui.activity.view.LoginView;
import com.friend.ui.util.BoxingGlideLoader;
import com.friend.ui.util.DesUtil;
import com.example.community.CommunityFragment;
import com.example.friend.FriendFragment;
import com.example.mine.MineFragment;
import com.example.news.NewsFragment;
import com.example.okhttputils.mvp.base.BaseActivity;
import com.example.okhttputils.mvp.base.BaseModel;
import com.example.okhttputils.mvp.ui.util.AppUtils;
import com.h.cheng.pselector.base.BaseMediaLoader;

import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import pub.devrel.easypermissions.EasyPermissions;

public class MainActivity extends BaseActivity<LoginPresenter> implements EasyPermissions.PermissionCallbacks, LoginView {
    @BindView(R.id.rb_main_school)
    RadioButton rbMainSchool;
    @BindView(R.id.rb_main_video)
    RadioButton rbMainVideo;
    @BindView(R.id.rb_main_friend)
    RadioButton rbMainFriend;
    @BindView(R.id.rb_main_my)
    RadioButton rbMainMy;
    private TextView textView;
    private NewsFragment fmNews;
    private CommunityFragment fmCommunity;
    private MineFragment fmMy;
    private FriendFragment fmFriend;
    private FragmentManager fragmentManager;
    private String title = " ";
    private User mUser;
    private UserDao mUserDao;
    private static final int RC_STORAGE = 1001;
    private String signature;
    private String model= "v1/login/login";
    private String key = "232dca354bb77411bfbcf67f6ca67c26";
    private String rand;
    private String phoneType;


    @Override
    protected LoginPresenter createPresenter() {
        return new LoginPresenter(this);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_main;
    }

    protected void initView() {

        requestPermision();
        BaseMediaLoader.getInstance().init(new BoxingGlideLoader());
        ARouter.getInstance().inject(this);
        fmNews = new NewsFragment().newInstance("这是资讯");
        fmCommunity = new CommunityFragment();
        fmFriend = new FriendFragment();
        fmMy = new MineFragment();
        fragmentManager = getSupportFragmentManager();
        fragmentManager.beginTransaction().add(R.id.ll_main, fmCommunity)
                .add(R.id.ll_main, fmNews).add(R.id.ll_main, fmMy).add(R.id.ll_main, fmFriend)
                .show(fmNews).hide(fmCommunity).hide(fmFriend).hide(fmMy).commit();
    }

    @Override
    protected void addListener() {
        getLogin();
    }

    private void getLogin() {
        String telephone = "18066583774";
        String telephone_code = "123";
        String phone_model = AppUtils.getPhoneType(MainActivity.this)+"";
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        rand = df.format(System.currentTimeMillis());
        try {
            signature = DesUtil.encrypt(model+key+rand);
            phoneType = "ChuangYeTianXia/1.4.0";
            Log.e("signature",signature+"");
        } catch (Exception e) {
            e.printStackTrace();
        }

        HashMap<String,String> map = new HashMap<>();
/*
        map.put("telephone","18066583774");
        map.put("telephone_code","123");
        map.put("mobile","0");
        map.put("version","1.4.0");
        map.put("phone_model", AppUtils.getPhoneType(MainActivity.this)+"");
        map.put("signature",signature);
*/
        map.put("data",signature);
        map.put("phone_type",phoneType);
        presenter.getLogin(map);
    }



    private void requestPermision() {
        String[] perms = {Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE,Manifest.permission.INTERNET,Manifest.permission.CAMERA,
        Manifest.permission.RECORD_AUDIO,Manifest.permission.LOCATION_HARDWARE};
        if (EasyPermissions.hasPermissions(this, perms)) {
            // Already have permission, do the thing
            try{
            }catch (Exception e){
                e.printStackTrace();
            }
            // ...
        } else {
            // Do not have permissions, request them now
            EasyPermissions.requestPermissions(this, "我们的app需要以下权限",
                    RC_STORAGE, perms);
        }
    }

    @OnClick({R.id.rb_main_school, R.id.rb_main_video, R.id.rb_main_friend, R.id.rb_main_my})
    public void onViewClicked(View view) {
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        switch (view.getId()) {
            case R.id.rb_main_school:
                ARouter.getInstance().build("/news/fragment")
                        .withString("news","资讯").navigation();
                transaction.show(fmNews).hide(fmCommunity).hide(fmFriend).hide(fmMy).commit();
                break;
            case R.id.rb_main_video:
                ARouter.getInstance().build("/community/fragment")
                        .withString("title","社群").navigation();
                transaction.show(fmCommunity).hide(fmNews).hide(fmFriend).hide(fmMy).commit();
                break;
            case R.id.rb_main_friend:
                transaction.show(fmFriend).hide(fmNews).hide(fmCommunity).hide(fmMy).commit();
                ARouter.getInstance().build("/friend/fragment")
                        .withString("friends","好友").navigation();
                break;
            case R.id.rb_main_my:
                transaction.show(fmMy).hide(fmNews).hide(fmFriend).hide(fmCommunity).commit();
                ARouter.getInstance().build("/mine/fragment")
                        .withString("mines","一笑的小馆子").navigation();
                break;
        }
    }


    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        // Forward results to EasyPermissions
        EasyPermissions.onRequestPermissionsResult(requestCode, permissions, grantResults, this);
    }

    @Override
    public void onPermissionsGranted(int requestCode, @NonNull List<String> perms) {
        // Some permissions have been granted
        try{
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    @Override
    public void onPermissionsDenied(int requestCode, @NonNull List<String> perms) {
        // Some permissions have been denied
    }

    @Override
    public void onLogin(BaseModel o) {

    }
}
